//
//  FullscreenController.swift
//  Fancyswap
//
//  Created by Francois Gratton on 2015-03-15.
//  Copyright (c) 2015 Jonathan Muschalle. All rights reserved.
//

import UIKit

class FullscreenController: UIViewController, UIScrollViewDelegate, UICollectionViewDelegate {

    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet var tapGesture: UITapGestureRecognizer!

    @IBOutlet weak var activitySpinner: UIActivityIndicatorView!
    @IBOutlet weak var Loading: UILabel!
    @IBOutlet weak var lblError: UILabel!
    
    var imageView = UIImageView()
    var lowResImage: UIImage?
    var fullResImage: UIImage?
    var fullResLoaded: Bool = false
    var filename: String = ""
    var lastSViewWidth: Int = 0
    var lastSViewHeight: Int = 0
    let fancyApi  = FancyRestApi()
    var isBlankImg: Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // must load the big image
        imageView.image = lowResImage
        
        if isBlankImg {
            Loading.hidden = false
            activitySpinner.startAnimating()
            activitySpinner.hidden=false
        }
        else{
            Loading.hidden = true
            activitySpinner.stopAnimating()
        }
        lblError.hidden = true

        let url = fancyApi.imageURL(filename, size: ImageFormat.Normal)
        
        fancyApi.GetDataFromUrl(url) {
            srcUrl, succeeded, msg, data in
            self.fullResLoaded = true
            let fn = srcUrl.lastPathComponent
            if self.filename.caseInsensitiveCompare(fn) != NSComparisonResult.OrderedSame {
                return
            }

            if succeeded
            {
                self.Loading.hidden = true
                self.activitySpinner.stopAnimating()
                self.lblError.hidden = true
                
                self.fullResImage = UIImage(data: data!)
                self.imageView.image = self.fullResImage
                self.imageView.bounds = CGRectMake(0,0, self.fullResImage!.size.width, self.fullResImage!.size.height)

                self.scaleImage()
            }
            else
            {
                self.Loading.hidden = true
                self.activitySpinner.stopAnimating()
                if self.isBlankImg {
                    self.lblError.hidden = false
                }
            }
        }
        imageView.backgroundColor = UIColor.redColor()
        imageView.userInteractionEnabled = true
        imageView.contentMode = UIViewContentMode.Center
        imageView.frame = CGRectMake(0,0, lowResImage!.size.width, lowResImage!.size.height)
        imageView.addGestureRecognizer(tapGesture)

        scrollView.addSubview(imageView)
        scrollView.contentSize = lowResImage!.size
        
        lastSViewWidth = Int(scrollView.frame.width)
        lastSViewHeight = Int(scrollView.frame.height)

    }
    func scaleImage()
    {        
        lastSViewWidth = Int(scrollView.frame.width)
        lastSViewHeight = Int(scrollView.frame.height)
        
        let vFrame = scrollView.frame
        let scaleWidth = vFrame.size.width / imageView.image!.size.width
        let scaleHeight = vFrame.size.height / imageView.image!.size.height
        let minScale = min(scaleHeight, scaleWidth)
        
        scrollView.minimumZoomScale = minScale
        scrollView.maximumZoomScale = 2
        scrollView.zoomScale = minScale
        
        CenterScrollViewContent()
    }
    override func viewDidLayoutSubviews() {
        if lastSViewWidth != Int(scrollView.frame.width) || lastSViewHeight != Int(scrollView.frame.height)
        {
            scaleImage()
        }
    }
    
    func CenterScrollViewContent(){
        let scrollViewSize = scrollView.bounds.size
        let imageSize = imageView.frame.size
        var contentsFrame = imageView.frame
        
        if imageSize.width < scrollViewSize.width{
            contentsFrame.origin.x = (scrollViewSize.width - imageSize.width) / 2
        }
        else {
            contentsFrame.origin.x = 0
        }
        
        if imageSize.height < scrollViewSize.height{
            contentsFrame.origin.y = (scrollViewSize.height - imageSize.height) / 2
        }
        else {
            contentsFrame.origin.y = 0
        }
        
        imageView.frame = contentsFrame
    }
    
    @IBAction func closeTap(sender: AnyObject) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    
    func scrollViewDidZoom(scrollView: UIScrollView) {
        CenterScrollViewContent()
    }
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return imageView
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
